/* global WebViewJavascriptBridge */
const setupWebViewJavascriptBridge = callback => {
  // 注册事件监听
  if (window.WebViewJavascriptBridge) {
    return callback(WebViewJavascriptBridge)
  } else {
    document.addEventListener('WebViewJavascriptBridgeReady', () => {
      callback(WebViewJavascriptBridge)
    }, false)
  }

  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback)
  }
  window.WVJBCallbacks = [callback]
  let WVJBIframe = document.createElement('iframe')
  WVJBIframe.style.display = 'none'
  WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
  document.documentElement.appendChild(WVJBIframe)
  setTimeout(function () {
    document.documentElement.removeChild(WVJBIframe)
  }, 0)
}

const showLog = (log) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showLog', log, function responseCallback(responseData) {
    })
  })
}

/* const doSetUserInfo = () => {
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      // bridge.registerHandler('doSetUserInfo', (data, responseCallback) => {
      //   store.commit('user/LOGIN_SUCCESS', JSON.parse(data));
      //   resolve(data);
      //   responseCallback('用户信息已收到');
      // })
      bridge.callHandler('getInuqireUserInfo', {action: 'getInuqireUserInfo'}, function responseCallback(responseCallback) {
        let resultJson = JSON.parse(responseCallback)
        console.log('===========resultJson.data====>' + resultJson.data)
        if (resultJson.isSuccess) {
          store.commit('user/LOGIN_SUCCESS', resultJson.data)
          resolve(resultJson.data)
          responseCallback('用户信息已收到')
        } else {
          console.log('===========error====>' + JSON.stringify(resultJson))
        }
      })
    })
  })
} */

const doBack = (param, jsonArgs) => {
  setupWebViewJavascriptBridge(function (bridge) {
    let objA = {action: 'doBack'}
    let objB = {action: 'doBack', param: param, jsonArgs: jsonArgs}
    bridge.callHandler('doBack', !param ? objA : objB, function responseCallback(responseData) {
    })
  })
}

const doDismiss = (param, jsonArgs) => {
  setupWebViewJavascriptBridge(function (bridge) {
    let objA = {action: 'doDismiss'}
    let objB = {action: 'doDismiss', param: param, jsonArgs: jsonArgs}
    bridge.callHandler('doDismiss', !param ? objA : objB, function responseCallback(responseData) {
    })
  })
}

const showLoginView = () => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showLoginView', {
      action: 'showLoginView',
      param: 'login'
    }, function responseCallback(responseData) {
    })
  })
}

const getCurrentVersion = () => {
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getCurrentVersion', {
        action: 'getCurrentVersion'
      }, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const getCurrentVersionCode = () => {
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getCurrentVersionCode', {
        action: 'getCurrentVersionCode'
      }, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const getCurrentAppLoadType = () => { // 获取app加载的本地的页面还是远程的页面(Local/Remote)
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getCurrentAppLoadType', {action: 'getCurrentAppLoadType'}, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const getCurrentBankId = () => { // 获取当前银行id
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getCurrentBankId', {action: 'getCurrentBankId'}, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const getIsLocalPkgVersion = () => { // 获取当前是否本地化包
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getIsLocalPkgVersion', {action: 'getIsLocalPkgVersion'}, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const getCurrentUseDomain = () => { // 获取当前使用的域名
  return new Promise((resolve, reject) => {
    setupWebViewJavascriptBridge(function (bridge) {
      bridge.callHandler('getCurrentUseDomain', {action: 'getCurrentUseDomain'}, function responseCallback(responseData) {
        let jsonObj = JSON.parse(responseData)
        if (jsonObj.isSuccess) {
          resolve(jsonObj.data)
        } else {
          reject(jsonObj.msg)
        }
      })
    })
  })
}

const goShowCalculator = (product, isBankId) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showCacletor', {
      action: 'showCacletor',
      jsonArgs: {'productId': isBankId ? product.bankProId : product.id, 'proVersion': product.version}
    }, function responseCallback(responseData) {})
  })
}

const goShowProductInfo = (product) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showProductInfo', {
      action: 'showProductInfo',
      param: product.id
    }, function responseCallback(responseData) {})
  })
}

const showWenJuan = (clearFlag) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showWenJuan', {
      action: 'showWenJuan',
      param: (clearFlag === 1)
    }, function responseCallback(responseData) {})
  })
}

const showDaiProduct = () => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('showDaiProduct', {action: 'showDaiProduct'}, function responseCallback(responseData) {})
  })
}

const goGetBankQrCode = () => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('goGetBankQrCode', {action: 'goGetBankQrCode'}, function responseCallback(responseData) {})
  })
}

const goChangePassword = () => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('goChangePassword', {action: 'goChangePassword'}, function responseCallback(responseData) {})
  })
}

const startDownloadApp = (newCodeName, appUrl) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('startDownloadApp', {action: 'startDownloadApp', param: {newCodeName: newCodeName, appUrl: appUrl}}, function responseCallback(responseData) {})
  })
}

const goSelectWork = () => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('goSelectWork', {action: 'goSelectWork'}, function responseCallback(responseData) {})
  })
}
// 进入预约页面
const goInquireApointPage = (parmas) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('kJSNativeInquireApointHandler', parmas, function responseCallback(responseData) {})
  })
}
// 进入调查页面
const goGoInquirePage = (parmas) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('kJSNativeGoInquireHandler', parmas, function responseCallback(responseData) {})
  })
}
// 进入已经完成页面
const goGoSurveyFinishPage = (parmas) => {
  setupWebViewJavascriptBridge(function (bridge) {
    bridge.callHandler('kJSNativeGoSurveyFinishHandler', parmas, function responseCallback(responseData) {})
  })
}

export default {
  setupWebViewJavascriptBridge,
  showLog,
  /* doSetUserInfo, */
  doBack,
  doDismiss,
  showLoginView,
  getCurrentVersion,
  goShowCalculator,
  goShowProductInfo,
  showWenJuan,
  showDaiProduct,
  goSelectWork,
  goInquireApointPage,
  goGoInquirePage,
  goGoSurveyFinishPage,
  goChangePassword,
  startDownloadApp,
  goGetBankQrCode,
  getCurrentVersionCode,
  getCurrentAppLoadType,
  getCurrentBankId,
  getIsLocalPkgVersion,
  getCurrentUseDomain
}
